"""
module contenant les fonction de securite
chiffrement, dechiffrement, decryptage etc
"""
import time
import pymongo
import re

def vigenere(phrase,clef,operation):
	#1=chiffre 2=dechiffre
	sortie, i = "", 0
	for caract in phrase:   #parcours de la chaine a traiter
		if operation == "1":    #chiffrement
			sortie = sortie + chr((ord(caract) + ord(clef[i])) % 256)
			i = i + 1   #parcours de la cle
			if i > len(clef) - 1:
				i = 0   #fin de cle atteinte, on repart au debut
		elif operation == "2":  #dechiffrement
			sortie = sortie + chr((ord(caract) - ord(clef[i])) % 256)
			i = i + 1
			if i > len(clef) - 1:
				i = 0
	return sortie

"""clef=input("entrer la clef:")
texte=input("entrer le texte:")
c=vigenere(texte,clef,"1")
print(c)
d=vigenere(c,clef,"2")
print(d)
"""

def trouver_anagrammes(base, lettres, n_lettres):
    if n_lettres == 0:
        yield base
    else:
        for l in lettres:
            if lettres[l] > 0:
                lettres[l] -= 1
                for an in trouver_anagrammes(base+l, lettres, n_lettres-1):
                    yield an
                lettres[l] += 1

def anagrammes(mot):
    res=[]
    res1=[]
    dico=open("dico/dico-fr.txt","r")
    lettres = dict((l, mot.count(l)) for l in set(mot))
    for anagramme in trouver_anagrammes('', lettres, len(mot)):
        res.append(anagramme)
    for mot in dico:
        for m in res:
            if mot.strip() == m:
                res1.append(mot)
    return res1

def anagrammes2(mot):
    res=[]
    res2=[]
    lettres = dict((l, mot.count(l)) for l in set(mot))
    for anagramme in trouver_anagrammes('', lettres, len(mot)):
        res.append(anagramme)
    for lettre in lettres:
        dico=open("dico/dico-fr-"+lettre+".txt","r")
        for mot in dico:
            for m in res:
                if mot.strip() == m:
                    res2.append(mot)
    return res2

def anagrammes3(mot):
    db=connection.dico
    res=[]
    res3=[]
    lettres = dict((l, mot.count(l)) for l in set(mot))
    for anagramme in trouver_anagrammes('', lettres, len(mot)):
        res.append(anagramme)
    cursor=db.dico.find({})
    for lettre in lettres:
        for d in cursor:
            for m in res:
                if d["mot"].strip() == m.strip():
                    res3.append(d["mot"])
    return res3

def anagrammes4(mot):
    res=[]
    res4=[]
    lettres = dict((l, mot.count(l)) for l in set(mot))
    for anagramme in trouver_anagrammes('', lettres, len(mot)):
        res.append(anagramme)
    for lettre in lettres:
        dic="dico"+lettre
        e="connection."+dic
        db=eval(e)
	f="db."+dic+".find({})"
	cursor=eval(f)
        for d in cursor:
            for m in res:
                if d["mot"].strip() == m.strip():
                    res4.append(d["mot"])
    return res4

def anagrammes5(mot):
    res=[]
    res5=[]
    dico=open("/dicoram/dico-fr.txt","r")
    lettres = dict((l, mot.count(l)) for l in set(mot))
    for anagramme in trouver_anagrammes('', lettres, len(mot)):
        res.append(anagramme)
    for mot in dico:
        for m in res:
            if mot.strip() == m:
                res5.append(mot)
    return res5

def anagrammes6(mot):
    res=[]
    res6=[]
    lettres = dict((l, mot.count(l)) for l in set(mot))
    for anagramme in trouver_anagrammes('', lettres, len(mot)):
        res.append(anagramme)
    for lettre in lettres:
        dico=open("/dicoram/dico-fr-"+lettre+".txt","r")
        for mot in dico:
            for m in res:
                if mot.strip() == m:
                    res6.append(mot)
    return res6

def anagrammes7(mot):
    global dicoa
    global dicob
    global dicoc
    global dicod
    global dicoe
    global dicof
    global dicog
    global dicoh
    global dicoi
    global dicoj
    global dicok
    global dicol
    global dicom
    global dicon
    global dicoo
    global dicop
    global dicoq
    global dicor
    global dicos
    global dicot
    global dicou
    global dicov
    global dicow
    global dicox
    global dicoy
    global dicoz
    res=[]
    res7=[]
    lettres = dict((l, mot.count(l)) for l in set(mot))
    for anagramme in trouver_anagrammes('', lettres, len(mot)):
        res.append(anagramme)
    for lettre in lettres:
        dico="dico"+lettre
        for mot in eval(dico):
            for m in res:
                if mot.strip() == m:
                    res7.append(mot)
    return res7


def cherche_anagramme(mot):
    start=time.time()
    a=anagrammes(mot)
    return a,time.time()-start

def cherche_anagramme2(mot):
    start=time.time()
    a=anagrammes2(mot)
    return a,time.time()-start

def cherche_anagramme3(mot):
    start=time.time()
    a=anagrammes3(mot)
    return a,time.time()-start

def cherche_anagramme4(mot):
    start=time.time()
    a=anagrammes4(mot)
    return a,time.time()-start

def cherche_anagramme5(mot):
    start=time.time()
    a=anagrammes5(mot)
    return a,time.time()-start

def cherche_anagramme6(mot):
    start=time.time()
    a=anagrammes6(mot)
    return a,time.time()-start

def cherche_anagramme7(mot):
    start=time.time()
    a=anagrammes7(mot)
    return a,time.time()-start


d=open("dico/dico-fr.txt","r")
dicoa=[]
dicob=[]
dicoc=[]
dicod=[]
dicoe=[]
dicof=[]
dicog=[]
dicoh=[]
dicoi=[]
dicoj=[]
dicok=[]
dicol=[]
dicom=[]
dicon=[]
dicoo=[]
dicop=[]
dicoq=[]
dicor=[]
dicos=[]
dicot=[]
dicou=[]
dicov=[]
dicow=[]
dicox=[]
dicoy=[]
dicoz=[]
for line in d:
    if re.search("^a",line):
        dicoa.append(line.strip())
    elif re.search("^b",line):
        dicob.append(line.strip())
    elif re.search("^c",line):
        dicoc.append(line.strip())
    elif re.search("^d",line):
       	dicod.append(line.strip())
    elif re.search("^e",line):
        dicoe.append(line.strip())
    elif re.search("^f",line):
        dicof.append(line.strip())
    elif re.search("^g",line):
        dicog.append(line.strip())
    elif re.search("^h",line):
        dicoh.append(line.strip())
    elif re.search("^i",line):
        dicoi.append(line.strip())
    elif re.search("^j",line):
        dicoj.append(line.strip())
    elif re.search("^k",line):
        dicok.append(line.strip())
    elif re.search("^l",line):
        dicol.append(line.strip())
    elif re.search("^m",line):
        dicom.append(line.strip())
    elif re.search("^n",line):
        dicon.append(line.strip())
    elif re.search("^o",line):
        dicoo.append(line.strip())
    elif re.search("^p",line):
        dicop.append(line.strip())
    elif re.search("^q",line):
        dicoq.append(line.strip())
    elif re.search("^r",line):
        dicor.append(line.strip())
    elif re.search("^s",line):
        dicos.append(line.strip())
    elif re.search("^t",line):
        dicot.append(line.strip())
    elif re.search("^u",line):
        dicou.append(line.strip())
    elif re.search("^v",line):
        dicov.append(line.strip())
    elif re.search("^w",line):
        dicow.append(line.strip())
    elif re.search("^x",line):
        dicox.append(line.strip())
    elif re.search("^y",line):
        dicoy.append(line.strip())
    elif re.search("^z",line):
        dicoz.append(line.strip())
print("debut")
while 1:
	mot=raw_input("Entrer le mot a anagrammer:")
	print("Resultats pour:")
	"""a,t=cherche_anagramme(mot)
	print("Algo initial:"+str(a)+"\n"+str(t)+"sec")"""
#	a,t=cherche_anagramme2(mot)
#	print("Algo initial+optimisation: "+str(a)+"\n"+str(t)+"sec")
	"""a,t=cherche_anagramme5(mot)
	print("Algo initial+fichier en ram:"+str(a)+"\n"+str(t)+"sec")
	a,t=cherche_anagramme6(mot)
	print("Algo initial+optimisation+fichier en ram: "+str(a)+"\n"+str(t)+"sec")
	connection = pymongo.Connection("localhost")
	a,t=cherche_anagramme3(mot)
	print("Utilisation mongodb: "+str(a)+"\n"+str(t)+"sec")
	a,t=cherche_anagramme4(mot)
	print("Utilisation mongodb+optimisation: "+str(a)+"\n"+str(t)+"sec")
	"""
	a,t=cherche_anagramme7(mot)
	print("Algo initial+variable: "+str(a)+"\n"+str(t)+"sec")
